﻿using ProductSystem.BLL.Common;
using ProductSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductSystem.BLL
{
    public partial class System_MenuBll
    {
        /// <summary>
        /// 菜单列表
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="parentId">父ID</param>
        /// <returns></returns>
        public static List<System_Menu> GetList(int status, int parentId = -1)
        {
            List<System_Menu> list = new List<System_Menu>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                if (status == -1 && parentId == -1)
                {
                    list = dbContext.System_Menus.Where(m => m.Status != 0).OrderBy(m => m.Menu_Order).ToList();
                }
                else if (status == -1 && parentId != -1)
                {
                    list = dbContext.System_Menus.Where(m => m.Status != 0 && m.Parent_ID == parentId).OrderBy(m => m.Menu_Order).ToList();
                }
                else
                {
                    list = dbContext.System_Menus.Where(m => m.Status == status).OrderBy(m => m.Menu_Order).ToList();
                }
            }
            return list;
        }

        /// <summary>
        /// 获取父菜单列表
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="parentId">父ID</param>
        /// <returns></returns>
        public static List<System_Menu> GetParentList(int status, int parentId)
        {
            List<System_Menu> list = new List<System_Menu>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                list = dbContext.System_Menus.Where(m => m.Status != 0 && m.Parent_ID == parentId).OrderBy(m => m.Menu_Order).ToList();
            }
            return list;
        }

        /// <summary>
        /// 生成菜单树
        /// </summary>
        /// <param name="menuId">菜单ID</param>
        /// <param name="type">0：默认，1：添加URL</param>
        /// <returns></returns>
        public static string GetMenuTreeJson(int menuId, List<System_Menu> SystemFunctionlist)
        {
            var list = SystemFunctionlist.Where(m => m.Parent_ID == menuId);
            StringBuilder sb = new StringBuilder();
            if (list.Count() < 1)
            {
                return "";
            }
            sb.Append(",\"children\":[");
            foreach (System_Menu model in list)
            {
                sb.Append("{");
                sb.AppendFormat("\"id\":\"{0}\",", model.ID);
                sb.AppendFormat("\"name\":\"{0}\"", model.Menu_Name);
                sb.Append(GetMenuTreeJson(model.ID, SystemFunctionlist).TrimEnd(','));
                sb.Append("},");
            }
            if (sb.ToString().EndsWith(","))
            {
                sb.Remove(sb.Length - 1, 1);
            }
            sb.Append("]");
            return sb.ToString();
        }

        /// <summary>
        /// 功能菜单树
        /// </summary>
        /// <param name="menuId">开始节点</param>
        /// <param name="typeId">类型</param>
        /// <returns>json对象</returns>
        public static string GetMenuTreeJson(int menuId, List<System_Menu> SystemFunctionlist, int typeId)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            foreach (System_Menu model in SystemFunctionlist)
            {
                sb.Append("{");
                sb.AppendFormat("\"id\":\"{0}\",", model.ID);
                sb.AppendFormat("\"pId\":\"{0}\",", model.Parent_ID);
                sb.AppendFormat("\"name\":\"{0}\"", model.Menu_Name);
                sb.Append("},");
            }
            if (sb.ToString().EndsWith(","))
            {
                sb.Remove(sb.Length - 1, 1);
            }
            sb.Append("]");
            return sb.ToString();
        }

        public static List<System_Menu> GetUserFuntionList(int userId)
        {
            List<System_Menu> listTemp = new List<System_Menu>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                System_User_Info user = dbContext.System_User_Infos.FirstOrDefault(m => m.ID == userId);
                if (user != null && user.Level == 0)
                {
                    listTemp = System_MenuBll.GetList(1);
                }
                else
                {
                    listTemp = dbContext.ExecuteQuery<System_Menu>("Exec Sp_User_Function " + userId).ToList();
                }
            }
            return listTemp;
        }
    }
}
